package com.webservice;

import java.io.*;
import sun.misc.BASE64Encoder;

public class Utils {
	public static byte[] getBytesFromFile(File file) {  
		       byte[] ret = null;  
		        try {  
		            if (file == null) {  
		                // log.error("helper:the file is null!");  
		                return null;  
		            }  
		            FileInputStream in = new FileInputStream(file);  
		            ByteArrayOutputStream out = new ByteArrayOutputStream(4096);  
		            byte[] b = new byte[4096];  
		            int n;  
		            while ((n = in.read(b)) != -1) {  
		                out.write(b, 0, n);  
		            }  
		            in.close();  
		            out.close();  
		            ret = out.toByteArray();  
		        } catch (IOException e) {  
		            // log.error("helper:get bytes from file process error!");  
		            e.printStackTrace();  
		        }  
		        return ret;  
	}
	
	public static byte[] getBytesFromInputStream(InputStream in) {  
	       byte[] ret = null;  
	        try {  
	            if (in == null) {  
	                // log.error("helper:the file is null!");  
	                return null;  
	            }   
	            ByteArrayOutputStream out = new ByteArrayOutputStream(4096);  
	            byte[] b = new byte[4096];  
	            int n;  
	            while ((n = in.read(b)) != -1) {  
	                out.write(b, 0, n);  
	            }  
	            in.close();  
	            out.close();  
	            ret = out.toByteArray();  
	        } catch (IOException e) {  
	            // log.error("helper:get bytes from file process error!");  
	            e.printStackTrace();  
	        }  
	        return ret;  
}
	
	public static String getBase64String(byte[] bty)
	{
		 BASE64Encoder encoder = new BASE64Encoder();
		 return encoder.encode(bty);//����Base64��������ֽ������ַ��� 
	}

}
